﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Components
{
    [EditorComponentType(ComponentType=typeof(EmailComponent))]
    public partial class EmailEditor : ComponentEditorBase
    {
        private bool initialized = false;

        public EmailEditor()
        {
            InitializeComponent();
            initialized = true;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.SaveSettings();
            if (this.ComponentUpdated != null)
                this.ComponentUpdated(this._Node, this._EmailComponent);

            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        #region Implementation of IComponentEditor
        private object _Node;
        private EmailComponent _EmailComponent;
        public override event OnComponentUpdated ComponentUpdated;
        public override void LoadSettings(object node, WorkflowComponentBase component)
        {
            this._Node = node;
            this._EmailComponent = (EmailComponent) component;

            EmailInstruction instruction = (EmailInstruction) this._EmailComponent.Instruction;
            this.ckUseOutlook.IsChecked = instruction.UseOutlook;
            this.txtFrom.Text = instruction.FromAddress;
            this.txtTo.Text = instruction.ToAddress;
            this.txtSubj.Text = instruction.Subject;
            this.ckAuthentication.IsChecked = instruction.RequireAuthentication;
            this.txtUserName.Text = instruction.UserName;
            this.txtPassword.Password = instruction.Password;
            this.ckSSL.IsChecked = instruction.EnableSSL;
            this.txtBody.Text = instruction.Body;
            this.lbAttachedFiles.Items.Clear();
            if(instruction.Attachments !=null && instruction.Attachments.Count>0)
            {
                foreach(string filePath in instruction.Attachments)
                {
                    this.lbAttachedFiles.Items.Add(filePath);
                }
            }
        }

        public override void SaveSettings()
        {
            EmailInstruction instruction = (EmailInstruction)this._EmailComponent.Instruction;
            instruction.UseOutlook = this.ckUseOutlook.IsChecked.Value;
            instruction.FromAddress = this.txtFrom.Text;
            instruction.ToAddress = this.txtTo.Text;
            instruction.Subject = this.txtSubj.Text;
            instruction.RequireAuthentication = this.ckAuthentication.IsChecked.Value;
            instruction.UserName = this.txtUserName.Text;
            instruction.Password = this.txtPassword.Password;
            instruction.EnableSSL = this.ckSSL.IsChecked.Value;
            instruction.Body = this.txtBody.Text;
            instruction.Attachments=new List<string>();
            if(this.lbAttachedFiles.Items.Count>0)
            {
                foreach(var item in this.lbAttachedFiles.Items)
                {
                    instruction.Attachments.Add(item.ToString());
                }
            }
        }

        #endregion

        private void ckAuthentication_Click(object sender, RoutedEventArgs e)
        {
            if(!initialized)
                return;

            if(this.ckAuthentication.IsChecked.Value)
            {
                this.txtUserName.IsEnabled = true;
                this.txtPassword.IsEnabled = true;
            }
            else
            {
                this.txtUserName.IsEnabled = false;
                this.txtPassword.IsEnabled = false;
            }
        }

        private void ckUseOutlook_Click(object sender, RoutedEventArgs e)
        {
            if(this.ckUseOutlook.IsChecked.Value)
            {
                this.txtSmtp.IsEnabled = false;
                this.txtFrom.IsEnabled = false;
                this.ckAuthentication.IsChecked = false;
                this.ckSSL.IsChecked = false;
                this.txtUserName.IsEnabled = false;
                this.txtPassword.IsEnabled = false;
            }
            else
            {
                this.txtSmtp.IsEnabled = true;
                this.txtFrom.IsEnabled = true;
            }
        }
    }
}

